package life.sweet.community.controller;

import life.sweet.community.cache.TagCache;
import life.sweet.community.dto.QuestionDTO;
import life.sweet.community.model.Question;
import life.sweet.community.model.User;
import life.sweet.community.service.QuestionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

@Controller
public class PublishController {
    @Autowired
    private QuestionService questionService;

    @GetMapping("/publish/{id}")
    public String edit(Model model, @PathVariable(name = "id") Long id) {
        QuestionDTO question = questionService.getById(id);
        model.addAttribute("title",question.getTitle());
        model.addAttribute("description",question.getDescription());
        model.addAttribute("tag",question.getTag());
        model.addAttribute("id",question.getId());
        model.addAttribute("tags", TagCache.get());
        return "publish";
    }

    @GetMapping("/publish")
    public String publish(Model model){
        model.addAttribute("tags", TagCache.get());
        return "publish";
    }

    @PostMapping("/publish")
    public String doPublish(
            @RequestParam(value="title", required=false) String title,
            @RequestParam(value="description", required=false) String description,
            @RequestParam(value="tag", required=false) String tag,
            @RequestParam(value="id", required=false) Long id,
            HttpServletRequest request,
            Model model
    ){
        model.addAttribute("title",title);
        model.addAttribute("id",id);
        model.addAttribute("description",description);
        model.addAttribute("tag",tag);
        model.addAttribute("tags", TagCache.get());

        User user = (User) request.getSession().getAttribute("user");
        if(user == null){
            model.addAttribute("error","用户未登录！");
            return "publish";
        }
        if(StringUtils.isEmpty(title)) {
            model.addAttribute("error","标题不能为空！");
            return "publish";
        }
        if(StringUtils.isEmpty(description)) {
            model.addAttribute("error","描述不能为空！");
            return "publish";
        }
        if(StringUtils.isEmpty(tag)) {
            model.addAttribute("error","标签不能为空！");
            return "publish";
        }
        String invalidTags = TagCache.filterValid(tag);
        if(StringUtils.isNotBlank(invalidTags)) {
            model.addAttribute("error","非法标签：" + invalidTags);
            return "publish";
        }

        Question question = new Question();
        question.setCreator(user.getId());
        question.setTag(tag);
        question.setTitle(title);
        question.setDescription(description);
        question.setId(id);
        questionService.insertOrUpdate(question);
        return "redirect:/";
    }
}
